Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sample application #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bclozel
Copy link

@bclozel bclozel commented Aug 16, 2020

Prior to this PR, the REST calls to external services would be performed twice:

  • once when calling flatMap from each step to get the next
  • and then another when calling Mono.zip on all those steps

As a general point, applications should avoid as much as possible chaining calls to external services as latency and issues tend to accumulate. If you can't get around this by caching or parallelizing calls, you could still consider other ways. This sample is very generic so I can't really advise something specific. But let's say one of those steps is about getting information from the HTTP header and checking credentials: you could externalize that cross-cutting concern into a WebFilter.

This PR is trying to simplify things and come up with a single reactive pipeline in the controller (i.e. a single input, and chaining calls with operators, returning the result). For that, we need to use Reactor's Tuples (or anything similar like apache commons, Vavr or others). Depending on the use case, we could come up with a better model design but it doesn't require specific reactive skills.

I'm getting the expected value and no duplicate logs when calling the service with httpie:

http POST http://localhost:8080/question thing=hello
HTTP/1.1 200 OK
Content-Length: 108
Content-Type: application/json

{
    "thingFromTheFirstStep": "firstOne",
    "thingFromTheSecondStep": "secondOne",
    "thingFromTheThirdStep": "thirdOne"
}

With sequential, external calls, the reactive approach could help in several ways:

  • better runtime performance with latency and threading; you could perform all calls using the same WebClient instance instead of creating a new one for each call.
  • better control over SLAs; you can use the timeout/retry and other related operators to improve the service behavior here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant